<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\house\HouseOrder as HouseOrderModel;
use app\api\model\house\HouseOrderComplain as HouseOrderComplainModel;
use addons\epay\library\Service;

class Houseorder extends Api
{
    protected $noNeedLogin = ['cancel','checkout','autocomplete'];
    protected $noNeedRight = ['*'];

    //详情
    public function pininfo()
    {
        $model = new HouseOrderModel();
        $order_id = input('order_id',0);

        $res = $model->getUserGroupOrderInfo($order_id,$this->auth->id);
        $this->success('',$res);
    }


    //详情
    public function changeinfo()
    {
        $model = new HouseOrderModel();
        $order_id = input('order_id',0);

        $res = $model->getUserOrderInfo($order_id,$this->auth->id);
        $this->success('',$res);
    }

    //订单列表
    public function lists()
    {
        $order_model = new HouseOrderModel();

        $data = [
            'user_id'=>$this->auth->id,
            'order_status'=>input('order_status','all'),
            'order_type'=>input('order_type',1)
        ];

        $list = $order_model->getUserOrderPageList($data);

        $this->success('',$list);
    }


    //详情
    public function info()
    {
        $model = new HouseOrderModel();
        $order_id = input('order_id',0);

        $res = $model->getUserOrderInfo($order_id,$this->auth->id);
        $this->success('',$res);
    }

    //取消订单
    public function cancel()
    {
        try {
            $model = new HouseOrderModel();
            $order_id = input('order_id',0);

            $res = $model->orderCancel($order_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);
    }

    //线下支付
    public function offlinepay()
    {
        $order_no = input('order_no','');
        $pay_type = input('pay_type','wechat');
        $model = new HouseOrderModel();

        $res = $model->notify($order_no,$pay_type);
        $this->success('提交成功',$res);
    }

    //支付
    public function pay()
    {
        $order_no = input('order_no','');
        $pay_type = input('pay_type','wechat');
        $method = input('method','app');
        $order = HouseOrderModel::get(['order_no'=>$order_no])        ;

        if (!$order){
            $this->error('订单不存在');
        }
        $request = request();
        $notifyurl =  $request->root(true)."/api/houseorder/notify";
        $title = '订购:'.$order['house_title'];
        try {
            $pay = Service::submitOrder($order['pay_money'],$order['order_no'],$pay_type,$title,$notifyurl,'',$method);
        }catch (\Exception $e) {
            // 回滚事务
            $this->error($e->getMessage());
        }

        $this->success('',$pay);
    }

    //回调
    public function notify()
    {
        $paytype = $this->request->param('paytype');
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            return json(['code' => 'FAIL', 'message' => '失败'], 500, ['Content-Type' => 'application/json']);
        }

        // 获取回调数据，V3和V2的回调接收不同
        $data = Service::isVersionV3() ? $pay->callback() : $pay->verify();

        try {
            //微信支付V3返回和V2不同
            if (Service::isVersionV3() && $paytype === 'wechat') {
                $data = $data['resource']['ciphertext'];
                $data['total_fee'] = $data['amount']['total'];
            }

            \think\Log::record($data);
            //获取支付金额、订单号
            $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
            $out_trade_no = $data['out_trade_no'];

            \think\Log::record("回调成功，订单号：{$out_trade_no}，金额：{$payamount}");

            //你可以在此编写订单逻辑

            $model = new HouseOrderModel();

            $res = $model->notify($out_trade_no,$paytype);
        } catch (\Exception $e) {
            \think\Log::record("回调逻辑处理错误:" . $e->getMessage(), "error");
        }

        //下面这句必须要执行,且在此之前不能有任何输出
        if (Service::isVersionV3()) {
            return $pay->success()->getBody()->getContents();
        } else {
            return $pay->success()->send();
        }

    }

    //已入住
    public function checkin()
    {
        try {
            $model = new HouseOrderModel();
            $order_id = input('order_id',0);

            $res = $model->orderUserCheckin($order_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);
    }

    //确认退房
    public function checkout()
    {
        try {
            $model = new HouseOrderModel();
            $order_id = input('order_id',0);

            $res = $model->orderUserCheckout($order_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('成功',$res);
    }

    //确认完成
    public function complete()
    {
        try {
            $model = new HouseOrderModel();
            $order_id = input('order_id',0);
            $res = $model->orderUserConfirm($order_id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('成功',$res);
    }

    //投诉页展示信息
    public function complainData()
    {
        $model = new HouseOrderComplainModel();
        $order_id = input('order_id',0);

        $info = $model->complainData($order_id);
        $this->success('成功',$info);
    }


    //投诉提交
    public function complain()
    {
        try {
            $model = new HouseOrderComplainModel();
            $order_id = input('order_id',0);
            $reason = input('reason','');
            $explain = input('explain','');
            $images = input('images','');
            $res = $model->complain($order_id,$reason,$explain,$images,$this->auth->id);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);

    }

    //自动完成
    public function autocomplete()
    {
        $model = new HouseOrderModel();
        $order_id = input('order_id',0);
        $res = $model->autocomplete($order_id);
        $this->success('',$res);
    }
}